package com.samsungmobile.common_module.SMS.Service;


import java.util.Properties;
import java.util.Vector;

import com.ipx.www.api.services.smsapi51.SmsApiPort;
import com.ipx.www.api.services.smsapi51.SmsApiServiceLocator;
import com.ipx.www.api.services.smsapi51.types.SendRequest;
import com.ipx.www.api.services.smsapi51.types.SendResponse;
import com.samsungmobile.common.ConfOta;
import com.samsungmobile.common.Logger;
import com.samsungmobile.common_module.SMS.MessageBean;
import com.samsungmobile.common_module.SMS.RequestBean;
import com.samsungmobile.security.UKDESEncryptDecrypt;

public class IPX extends OtaService {

	public IPX() {
		this.serviceName = "ipx";
		Logger.LOG_NOHUP.info("=============IPX=====================");
	}
	
	@Override
	protected void setProperties(RequestBean rb, MessageBean mb) {
		
		String[] saCountry = ConfOta.getArray("countries");
		String sCountry = "";
		
		for ( int i=0; i < saCountry.length; i++){
			
			if( rb.getMsisdn().startsWith(saCountry[i]) ){
				sCountry = saCountry[i];
				break;
			}
		}

		String sCountryCode = ConfOta.getConf(sCountry+".code");
		mb.setCountryCode( sCountryCode );
		
		UKDESEncryptDecrypt ukdes = new UKDESEncryptDecrypt();
		
		mb.setId( ukdes.decrypt(ConfOta.getConf(sCountry+".id")) );
		mb.setPw( ukdes.decrypt(ConfOta.getConf(sCountry+".pw")) );
		
		if( !rb.getSmsType().equals("SMS") ){
			mb.setDcs(ConfOta.getInt(sCountry+"."+rb.getOperator()+".dcs"));
		}else{
			mb.setDcs(-1);
		}
		
		mb.setOperator(rb.getOperator());
		mb.setDa( rb.getMsisdn() ); 
	    mb.setOa( rb.getOriginatingAddress() );
	    
	    String sRegex = "^[0-9]+$";
	    //number type
	    if( rb.getOriginatingAddress().matches(sRegex))
	    	mb.setOt(0);
	    //alpha numeric
	    else
	    	mb.setOt(1);
	    
	    mb.setReferenceId(rb.getReferenceId());
	}
	
	/**
	 * Sends a SMS to IPX and returns the IPX response
	 * @param mb
	 * @return
	 * @throws Exception
	 */
	protected SendResponse[] invoke(MessageBean mb) throws Exception {

        //Get locator and port
		SmsApiServiceLocator aLocator = new SmsApiServiceLocator();
        SmsApiPort aSmApi = null;
        aSmApi = aLocator.getSmsApi51();

		//Send WAP Push and get result
		SendResponse[] soapResponse = new SendResponse[mb.getMessageCount()];

		
        for( int i=0; i < mb.getMessageCount() ; i++ ){

            //Make request
        	SendRequest soapRequest = new SendRequest();
        	soapRequest.setCorrelationId("");
        	soapRequest.setOriginatingAddress( mb.getOa() );
        	soapRequest.setOriginatorTON( mb.getOt() );
        	soapRequest.setDestinationAddress(mb.getDa());
        	soapRequest.setUserData( ((Properties)mb.getMessage().get(i)).getProperty("BODY") );
        	soapRequest.setUserDataHeader( ((Properties)mb.getMessage().get(i)).getProperty("HEADER") );
            
            soapRequest.setDCS(mb.getDcs());  
            soapRequest.setPID(-1);
            soapRequest.setRelativeValidityTime(-1);
            soapRequest.setDeliveryTime("#NULL#");
            soapRequest.setStatusReportFlags(-1);
            soapRequest.setAccountName("");

            String sTarriffClass = mb.getCountryCode().equals("gbr")? "GBP0" : "EUR0";
            if ( mb.getCountryCode().equals("esp")){
                if (mb.getOperator().equals("amena") || mb.getOperator().equals("airtel")){
                	sTarriffClass = "EUR017";
            	}	
            }
            soapRequest.setTariffClass(sTarriffClass);
        	
            soapRequest.setVAT(-1);
            soapRequest.setReferenceId("");
            soapRequest.setContentCategory("#NULL#");
            
            soapRequest.setContentMetaData("");
            soapRequest.setUsername(mb.getId());
            soapRequest.setPassword(mb.getPw());
            // Reference id added (2010-06-17 Andreas Happ)
            soapRequest.setReferenceId(mb.getReferenceId());
            
            // Send request
            soapResponse[i] = aSmApi.send(soapRequest);
/*
            String sCorelationId = soapResponse[i].getCorrelationId();
        	String sMessageId = soapResponse[i].getMessageId();
        	int iResponseCode = soapResponse[i].getResponseCode();
        	int iReasonCode = soapResponse[i].getReasonCode();
        	String sResponseMessage = soapResponse[i].getResponseMessage();
        	boolean bTemporaryError = soapResponse[i].isTemporaryError();
        	int iBillingStatus = soapResponse[i].getBillingStatus();
        	double dVAT = soapResponse[i].getVAT();
        	
        	System.out.println("======================= SamsungMobile =================");
        	System.out.println("BODY\t\t\t= "+((Properties)mb.getMessage().get(i)).getProperty("BODY"));
        	System.out.println("HEADER\t\t\t= "+((Properties)mb.getMessage().get(i)).getProperty("HEADER"));
        	System.out.println("sCorelationId\t\t\t= "+sCorelationId);
        	System.out.println("sMessageId\t\t\t= "+sMessageId);
        	System.out.println("iResponseCode\t\t\t= "+iResponseCode);
        	System.out.println("iReasonCode\t\t\t= "+iReasonCode);
        	System.out.println("sResponseMessage\t\t= "+sResponseMessage);
        	System.out.println("bTemporaryError\t\t\t= "+bTemporaryError);
        	System.out.println("iBillingStatus\t\t\t= "+iBillingStatus);
        	System.out.println("dVAT\t\t\t\t= "+dVAT);
        	System.out.println("======================= SamsungMobile =================");
*/
        }
        
        return soapResponse;

	}
	
	protected SendResponse[] sendText(RequestBean rb, MessageBean mb) throws Exception {

		Vector<Properties> vector = new Vector<Properties>();

	    Properties p = new Properties();
	    p.setProperty("BODY", rb.getMessage());
	    p.setProperty("HEADER", "#NULL#");
	    vector.add(p);
		
	    mb.setMessage(vector);
	    mb.setMessageCount(1);
	    
		return invoke(mb);
	}
	

}
